package com.yangrong.newspublicservice.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yangrong.newspublicservice.entity.TbHomepageMenuEntity;
import com.yangrong.newspublicservice.service.TbHomepageMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yangrong.common.utils.PageUtils;
import com.yangrong.common.utils.R;


/**
 * 主页导航菜单
 *
 * @author yangrong
 * @email 455833224@qq.com
 * @date 2020-09-12 16:19:40
 */
@RestController
@RequestMapping("public/tbhomepagemenu")
public class TbHomepageMenuController {
    @Autowired
    private TbHomepageMenuService tbHomepageMenuService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 获取网站导航栏
     *
     * @param params
     * @return
     */
    @RequestMapping("/getMenuList")
    public R getMenuList(@RequestParam Map<String, Object> params) {
        List<TbHomepageMenuEntity> list = tbHomepageMenuService.getMenuList(params);

        return R.ok().put("menuList", list);
    }

    /**
     * 获取网站导航栏
     *
     * @param params
     * @return
     */
    @RequestMapping("/getHzValue")
    public R getHzValue(@RequestParam Map<String, Object> params) {
        String Hz = stringRedisTemplate.opsForValue().get("HzValue");
        if (Hz != null && Hz.equals("")) {
            return R.ok().put("HzValue", Hz);
        } else {
            return R.ok().put("HzValue", 50);
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tbHomepageMenuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TbHomepageMenuEntity tbHomepageMenu = tbHomepageMenuService.getById(id);

        return R.ok().put("tbHomepageMenu", tbHomepageMenu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TbHomepageMenuEntity tbHomepageMenu) {
        tbHomepageMenuService.save(tbHomepageMenu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbHomepageMenuEntity tbHomepageMenu) {
        tbHomepageMenuService.updateById(tbHomepageMenu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tbHomepageMenuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
